package com.augurit.wf.jbpmex.cmd;

import org.jbpm.api.cmd.Environment;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.el.Expression;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.task.TaskDefinitionImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.augurit.wf.common.util.WfUtils;
import com.augurit.wf.jbpmex.task.multitask.proxy.IModeProxy;
import com.augurit.wf.jbpmex.task.multitask.proxy.MultiTaskProxyFactory;

public class CreateSubMultiTasksInRumtimeCmd extends AbstractCommand<Void>{
	private static final long serialVersionUID = -5996415196851997153L;
	
	protected String assignee;
	protected TaskImpl superTask;
	
	public CreateSubMultiTasksInRumtimeCmd(String assignee, TaskImpl superTask){
		this.assignee = assignee;
		this.superTask = superTask;
	}
	
	public Void execute(Environment environment) throws Exception {
		
		if(WfUtils.isMultiTask(superTask) && assignee != null && assignee.trim().length() > 0){
			ExecutionImpl execution = superTask.getExecution();
			ProcessDefinitionImpl pdf = execution.getProcessDefinition();
			TaskDefinitionImpl taskDefinition = pdf.getTaskDefinition(superTask.getActivityName());
			
			//多工作项模式实现
			IModeProxy proxy = MultiTaskProxyFactory.getProxy(taskDefinition, execution);
			proxy.createSubTasksInRumtime(superTask, Expression.create(assignee));
		}
		
//		int index = getNewSubTaskIndex(environment);
//		proxy.createSubTask(superTask, index, assignee);
		
		return null;
	}
	
	//获取多工作项的优先级
//	private int getNewSubTaskIndex(Environment environment){
//		int index = 0;
//		HistoryService historyService = environment.get(HistoryService.class);
//		List<HistoryTask> list = historyService.createHistoryTaskQuery().executionId(execution.getId()).list();
//		if(list != null && list.size() >0)
//			index = list.size();
//		return index;
//	}
	
}
